package 链表;

public class 环形链表 {
    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }

        ListNode firstNode = head;
        ListNode secondNode = head;

        while (true) {
            if (firstNode.next == null || secondNode.next == null || secondNode.next.next == null) {
                return false;
            }
            firstNode = firstNode.next;
            secondNode = secondNode.next.next;
            if (firstNode == secondNode) {
                return true;
            }

        }

    }

    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

}


